# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.admin.food.canteen import Canteen
from web.models.admin.food.box import Box
import time

@route(r'/admin/food/box/?', name="box.index")
class IndexHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        page = (int(cur_page) - 1) * self.offset

        ret, count = yield Box().select_box(school_id, page, self.offset)

        page_info = {'total': count, 'page': int(cur_page)}
        self.render("admin/food/box/list.html", page_info=page_info, data=ret)


@route(r'/admin/food/box/new/?')
class NewHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        canteen = yield Canteen().select_all_canteen(school_id)

        self.render("admin/food/box/new.html", canteen=canteen)

    @asynchronous
    @gen.coroutine
    def post(self):
        ltype = self.get_argument('ltype', default='0')
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fmin_price = self.get_argument('Fmin_price', default='')
        Fcanteen_id = self.get_argument('Fcanteen_id', default='')
        Fremark = self.get_argument('Fremark', default='')

        box_info_data = {
            "Fname":         Fname,
            "Fmin_price":   Fmin_price,
            "Fcanteen_id":        Fcanteen_id,
            "Fremark":         Fremark,
            "Fschool_id":    school_id,
            "Fcreate_time":  time.time(),
            "Fstatus":       '0',
        }

        count = yield Box().insert_box(box_info_data)

        if ltype == '0':
            self.redirect('/admin/food/box/')
        else:
            self.redirect('/admin/food/box/new/')

@route(r'/admin/food/box/del/(?P<FboxId>[0-9].*)/(?P<cur_page>[0-9].*)', name='box.del_box')
class DelHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, FboxId, cur_page):
        school_id = self.get_argument('schoolId', default=self.school_id)

        ret = Box().del_box(FboxId)

        self.redirect('/admin/food/box/?page=' + cur_page)

@route(r'/admin/food/box/edit/(?P<Fid>[0-9].*)', name='box.edit')
class EditHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)

        box = yield Box().find_box(Fid)
        canteen = yield Canteen().select_all_canteen(school_id)

        self.render("admin/food/box/edit.html", box=box, canteen=canteen)

    @asynchronous
    @gen.coroutine
    def post(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fmin_price = self.get_argument('Fmin_price', default='')
        Fcanteen_id = self.get_argument('Fcanteen_id', default='')
        Fremark = self.get_argument('Fremark', default='')

        box_info_data = {
            "Fname":         Fname,
            "Fmin_price":   Fmin_price,
            "Fcanteen_id":        Fcanteen_id,
            "Fremark":         Fremark,
            "Fschool_id":    school_id,
        }

        count = yield Box().update_box(Fid, box_info_data)

        self.redirect('/admin/food/box/')

@route(r'/admin/food/box/istatus/(?P<cur_page>[0-9].*)/(?P<Fstatus>[0-9].*)', name='box.set_istatus')
class IstatusHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def post(self, cur_page, Fstatus):
        school_id = self.get_argument('schoolId', default=self.school_id)
        data = self.get_argument('data', default='')

        ret = yield Box().set_istatus(Fstatus, *(data.split(',')))

        self.write(str(ret))

        # self.redirect('/admin/food/box/?page=' + cur_page)


@route(r'/admin/food/box/status/(?P<Fid>[0-9].*)/(?P<Fstatus>[0-9].*)/(?P<cur_page>[0-9].*)', name='box.set_status')
class StatusHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, Fid, Fstatus, cur_page):
        school_id = self.get_argument('schoolId', default=self.school_id)

        ret = yield Box().set_status(Fid, Fstatus)

        self.redirect('/admin/food/box/?page=' + cur_page)